package com.mvc.action;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mvc.form.ActionForm;

public abstract class Action<T> {
	//public abstract String execute(HttpServletRequest req, HttpServletResponse resp, ActionForm form);
	
	public static Action getAction(Class<?> clazz){
		Action action = null;
		try {
			action = (Action) clazz.newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return action;
	}
	
	public static String getResult(Class<?> clazz, String methodName, HttpServletRequest req, HttpServletResponse resp, ActionForm form){
		String result = null;
		try {
			Action action = (Action) clazz.newInstance();
			Method method = clazz.getMethod(methodName, new Class[]{HttpServletRequest.class, HttpServletResponse.class, ActionForm.class});
			result = (String) method.invoke(action, req, resp, form);
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return result;
	}
}
